import Vue from 'vue'
import axios from 'axios'
import 'babel-polyfill'

export const http = axios.create({
  // baseURL: API.baseURL,
  headers: {
    'Content-Type': 'application/json'
  },
  withCredentials: true
})

// 请求拦截
http.interceptors.request.use(config => {
  if (config.method === 'get') {

  } else {
    if (config.data) {
      config.data = JSON.stringify(config.data)
    }
  }
  return config
}, err => {
  return Promise.reject(err)
})

// 响应拦截
http.interceptors.response.use(res => {
  const data = res.data

  if (typeof data === 'undefined') {
    return Promise.reject(data)
  }

  return res
}, err => {
  return Promise.reject(err.response)
})

export default {
  install () {
    Vue.prototype.$http = http
  }
}
